package jrmob.yonyou.com.commonlib.app_config_tool.mobapprove;

import android.content.Context;
import android.text.TextUtils;

import com.yonyou.uap.um.service.ServiceCallback;
import com.yonyou.uap.um.service.ServiceProxy;

import org.json.JSONObject;

import java.util.HashMap;

import jrmob.yonyou.com.commonlib.baseui.base.BaseApp_lib;
import jrmob.yonyou.com.commonlib.login.bean.MobileDevice_lib;
import jrmob.yonyou.com.commonlib.app_config_tool.constant.MAServerInfo;
import jrmob.yonyou.com.commonlib.app_config_tool.constant.NetUtilContant;
import jrmob.yonyou.com.commonlib.app_config_tool.tool.base.LogUtils;

/**
 * 移动审批
 */
public class MAServices_ma {

	private static Context ctx;

	public enum ServiceType {
		ncLoginService, umCommonService, maService
	}

	// 调用普通http请求
	public static void callMACommonService(Context context, ServiceCallback serviceCallback,
			JSONObject requestParam) throws Exception {

		ctx = context;
		String maRequestUrl = initMaRequestUrl();

		HashMap<String, String> maParam = new HashMap<String, String>();

		handlerParam(maParam, requestParam, context);

		new ServiceProxy(context).start(maRequestUrl, maParam, serviceCallback, false, 20000);
	}

	private static void handlerParam(HashMap<String, String> maParam, JSONObject requestParam,
			Context context) throws Exception {
		ctx = context;
		maParam.put("tp", "des");// 加密传输
		JSONObject requestJson = new JSONObject();
		requestJson.put("serviceid", ServiceType.umCommonService);

		requestJson.put("deviceinfo", MobileDevice_lib.getDeviceInfo(context));

		JSONObject appContext = generateAppContext(context);

		requestJson.put("appcontext", appContext);

		JSONObject serviceContext = generateServiceContext(requestParam);

		requestJson.put("servicecontext", serviceContext);

		maParam.put("data", requestJson.toString());

	}

	private static JSONObject generateServiceContext(JSONObject requestParam) throws Exception {
		JSONObject json = new JSONObject();
		json.put("actionid", ServiceType.umCommonService);
		json.put("callback", "nothing");
		json.put("viewid", MAServerInfo.controller_approve);
		json.put("controllerid", MAServerInfo.controller_approve);
		json.put("actionname", MAServerInfo.actionName_approve);
		json.put("transtype", "currency");
		json.put("params", requestParam);

		return json;
	}

	private static JSONObject generateAppContext(Context context) throws Exception {
		JSONObject json = new JSONObject();
		ctx = context;
		json.put("devid", MobileDevice_lib.getDeviceInfo(context).opt("deviceid"));// add
																				// 设备id
		json.put("appid", MAServerInfo.appid_approve);
		// json.put("token", "");
		// json.put("userid","");
		// json.put("sessionid","");
		// json.put("user", "");
		// json.put("pass", "");
		// json.put("groupid", "");
		// json.put("funcid", "");
		// json.put("funcode", "");
		// json.put("massotoken", "");
		// json.put("tabid", "");
		return json;
	}

	// 初始化MA服务器URL
	private static String initMaRequestUrl() {

		String maIP = BaseApp_lib.getIp_ma();
		if(TextUtils.isEmpty(maIP)){// 内存重启时可能会导致ip获取不到，故当ip为空时手动赋值
			maIP = NetUtilContant.ip_ma;
		}
		String maPort = BaseApp_lib.getPort_ma();
		if(TextUtils.isEmpty(maPort)){// 内存重启时可能会导致获取不到，故当port为空时手动赋值
			maPort = NetUtilContant.port_ma;
		}
		LogUtils.d("initMaRequestUrl: maIP = " + maIP + " maPort = " + maPort);
		StringBuffer urlBuffer = new StringBuffer();
		urlBuffer.append("http://").append(maIP).append(":").append(maPort)
				.append("/umserver/core/");
		return urlBuffer.toString();
	}

	// 调用上传服务
	public static void callMaUpLoadService() {

	}
}
